package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class GetUsers
 */
//@WebServlet(description = "選取該角色裡的人員", urlPatterns = { "/GetUsers" })
public class GetUsers extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetUsers() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/* 變數宣告 */
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String userSpecified = request.getParameter("getUserType");
		String strSQL = "";
		if (!userSpecified.equalsIgnoreCase("ALL")) {
			strSQL = "SELECT userID, pwd, nickName, lastName, firstName, email FROM tms_personnel WHERE userID='"+userSpecified+"'";
		} else {
			strSQL = "SELECT userID, pwd, nickName, lastName, firstName, email FROM tms_personnel";
		}
		
		String returnResult = "";
		ArrayList<User> users = new ArrayList<User>();
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			while (rs.next()) {
				User user = new User();
				ArrayList<Role> roles = new ArrayList<Role>();
				if (!userSpecified.equalsIgnoreCase("ALL")) {
					strSQL = "SELECT t2.roleID, t2.roleName FROM tms_roleusermapping t1, tms_role t2 WHERE t1.roleID = t2.roleID AND t1.userID =?";
					ps = conn.prepareStatement(strSQL);
					ps.setString(1, userSpecified);
					rs2 = ps.executeQuery();
					while (rs2.next()){
						Role role = new Role();
						role.setRoleID(rs2.getInt("roleID"));
						role.setRoleName(rs2.getString("roleName"));
						roles.add(role);	
					}
				}
				user.setUserID(rs.getString("userID"));
				user.setPwd(rs.getString("pwd"));
				user.setFirstName(rs.getString("firstName"));
				user.setLastName(rs.getString("lastName"));
				user.setNickName(rs.getString("nickName"));
				user.setEmail(rs.getString("email"));
				user.setRoles(roles);
				users.add(user);
			}
			returnResult = new Gson().toJson(users);
		} catch (Exception e) {
			returnResult = wid.keepErrorMes(conn, ps, e,"", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (rs2!= null) {rs2.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString();
			}
		}

		/* 設定要回傳的格式 */
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);

	}
}
